package XynLeen.GraduationProject.view;

import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;

import XynLeen.GraduationProject.R;
import butterknife.Bind;
import butterknife.ButterKnife;

public abstract class BaseRecycleListActivity<T>  extends BaseActivity {

    @Bind(R.id.recyclerview)
    RecyclerView mRecyclerview;
    public CommonRecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initAdapter();
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        mRecyclerview.setLayoutManager(getlayoutManager());
        mRecyclerview.addItemDecoration(new SpacesItemDecoration(2,2));
        mRecyclerview.setAdapter(mAdapter);

    }


    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int leftRight;
        private int topBottom;

        public SpacesItemDecoration(int leftRight, int topBottom) {
            this.leftRight = leftRight;
            this.topBottom = topBottom;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            //竖直方向的
            if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
                //最后一项需要 bottom
                if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                    outRect.bottom = topBottom;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
                outRect.right = leftRight;
            } else {
                //最后一项需要right
                if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                    outRect.right = leftRight;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
                outRect.bottom = topBottom;
            }

        }
    }

    private void initAdapter() {

        mAdapter = new CommonRecyclerAdapter<T>(this,getAdapterItemId(),null) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, final T item, final int position) {
                doConvert(helper,item,position);
            }
        };
    }

    public  RecyclerView.LayoutManager getlayoutManager(){
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return layoutManager;
    }


    public abstract void doConvert(BaseAdapterHelper helper, T item,int position);

    public abstract int getAdapterItemId();


}
